package com.vueblog.webapi.controller;


import com.vueblog.webapi.exception.BusinessException;
import com.vueblog.webapi.request.BasePageRequest;
import com.vueblog.webapi.request.CreateOrUpdateBlogRequest;
import com.vueblog.webapi.request.GetBlogInfoByWordRequest;
import com.vueblog.webapi.request.GetBlogInfoPageRequest;
import com.vueblog.webapi.response.BasePageResponse;
import com.vueblog.webapi.response.BaseResponse;
import com.vueblog.webapi.service.IBlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author luosj
 * @since 2021-05-15
 */
@Controller
@CrossOrigin
@RequestMapping("/show/blogs")
public class ShowBlogController extends BaseController {

    @Autowired
    private IBlogService blogService;

    /**
     * 分页模糊查询博客
     */
    @PostMapping("/list")
    @ResponseBody
    public BasePageResponse getBlogInfoPageList(@RequestBody @Valid GetBlogInfoPageRequest request, BindingResult bindingResult) {
        checkBindingResult(bindingResult);
        return blogService.getBlogInfoPageList(request);
    }

    /**
     * 获取发布且推荐博客
     */
    @GetMapping("/pAndRList")
    @ResponseBody
    public BaseResponse getPublishedAndRecommendBlogList() {
        return blogService.getPublishedAndRecommendBlogList();
    }

    /**
     * 根据博客id获取博客
     */
    @GetMapping("/blogInfo/{id}")
    @ResponseBody
    public BaseResponse getBlogByid(@PathVariable("id") Long id) {
        return blogService.getBlogByid(id);
    }

    /**
     * 根据分类id获取博客
     */
    @PostMapping("/byType/{id}")
    @ResponseBody
    public BasePageResponse getBlogByTypeId(@RequestBody @Valid BasePageRequest request, @PathVariable("id") Long id, BindingResult bindingResult) {
        checkBindingResult(bindingResult);
        return blogService.getBlogByTypeId(request, id);
    }

    /**
     * 归档
     */
    @GetMapping("/archive")
    @ResponseBody
    public BaseResponse getArchiveBlogs() {
        return blogService.archiveBlog();
    }

    /**
     * 根据标签id获取博客
     */
    @PostMapping("/byTag/{id}")
    @ResponseBody
    public BasePageResponse getBlogByTagId(@RequestBody @Valid BasePageRequest request, @PathVariable("id") Long id, BindingResult bindingResult) {
        checkBindingResult(bindingResult);
        return blogService.getBlogByTagId(request, id);
    }

    /**
     * 根据关键字获取博客
     */
    @PostMapping("/byWord")
    @ResponseBody
    public BasePageResponse getBlogByWord(@RequestBody @Valid GetBlogInfoByWordRequest request, BindingResult bindingResult) {
        checkBindingResult(bindingResult);
        return blogService.getBlogByWord(request);
    }
}
